package core

import (
	"context"
	"fmt"
	"gin_framework/global"
	"github.com/go-redis/redis"
	"time"
)

var rdb *redis.Client

func ConnectRedis() *redis.Client {
	redisConfig := global.Config.Redis
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisConfig.Addr(),
		DB:       redisConfig.DB,
		PoolSize: redisConfig.PoolSize,
	})
	_, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()
	_, err := rdb.Ping().Result()
	if err != nil {
		fmt.Println(err)
		return nil
	}
	return rdb
}
